package com.mjk.manager.centerconf.type.impl;

import java.util.Map;

import com.mjk.common.base.scope.BeanRefreshScope;
import com.mjk.common.core.redis.datatype.RedisUtils;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.tools.springextend.BeeSpringBean;
import com.mjk.common.util.ComputerUtils;
import com.mjk.manager.centerconf.conf.CenterConf;
import com.mjk.manager.centerconf.dto.RefreshEntity;
import com.mjk.manager.centerconf.type.RefreshInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
 * 普通类属性刷新
 * @author jafek
 *
 */
@Component
public class DefaultFiledRefreshImpl implements RefreshInterface {
	@Autowired
	private RedisTemplate redisTemplate;
	@Override
	public void refresh(AbstractApplicationContext context,Map<String,Object> refreshData,Map<String,Object> map) {
		//判断环境信息
		RefreshEntity entity = (RefreshEntity) RedisUtils.get(redisTemplate, CenterConf.CONFIGURATION_KEY);
		BizLogger.info(ComputerUtils.getIpAddress());
		//如果环境一样，更新
		if(BeeSpringBean.getActiveProfile().equals(entity.getProfile())){
			BizLogger.info(entity);
			map.putAll(refreshData);
			MapPropertySource mailPropertySource = new MapPropertySource(CenterConf.THRIDAPPLICATION_SOURCE_NAME, map);
			context.getEnvironment().getPropertySources().addFirst(mailPropertySource);
			BeanRefreshScope.clean();
		}
	}
}
